package com.carcontrol.myplugin4.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.fragment.app.FragmentActivity;

import com.carcontrol.myplugin4.core.common.AppManager;
import com.carcontrol.myplugin4.core.widget.LoadingDialog;
import com.hongx.pluginstand.PayInterfaceActivity;

import java.lang.ref.WeakReference;

public class BaseFragmentActivity extends FragmentActivity
		implements PayInterfaceActivity
{
	protected Context context;
	protected LoadingDialog loadingDialog;

	private static final int MSG_SET_ALIAS = 1001;
	private static final int MSG_SET_TAGS = 1002;

	private TelephonyManager tm;

	protected boolean isRefresh;

	protected Handler m_myHandler = new MainHandler(this);

	protected Activity that;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if(that == null){
			super.onCreate(savedInstanceState);
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);		//设定为竖屏
			tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
		}else{

		}


		context = this;
		BaseApplication.context = context;




		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
//		// 修改状态栏颜色，4.4+生效
//		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
//			// 透明状态栏
//			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			// 透明导航栏
//			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//		}

		loadingDialog = new LoadingDialog(context);
		loadingDialog.setCancelable(false);
		loadingDialog.setCanceledOnTouchOutside(false);

	}



    @Override
    public void setContentView(int layoutResID) {
        Log.e("bfa===setContentView2", layoutResID+"==="+that);

        if(that != null){
            that.setContentView(layoutResID);
        }else{
            super.setContentView(layoutResID);
        }
    }
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// 结束Activity从堆栈中移除
		AppManager.getAppManager().finishActivity(this);
	}

	@Override
	public void attach(Activity proxyActivity) {
		this.that = proxyActivity;
	}

	@Override
	public <T extends View> T findViewById(int id) {
		if(that != null){
			return that.findViewById(id);
		}else{
			return super.findViewById(id);
		}
	}




	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	public void finishMine() {
		AppManager.getAppManager().finishActivity(this);
	}

	public void onStartCommon(final String title) {
		m_myHandler.post(new Runnable() {
			@Override
			public void run() {
				if (loadingDialog != null && !loadingDialog.isShowing()) {
					loadingDialog.setTitle(title);
					loadingDialog.show();
				}
			}
		});

	}


	class MainHandler extends Handler {
		WeakReference<BaseFragmentActivity> softReference;

		public MainHandler(BaseFragmentActivity activity) {
			softReference = new WeakReference<BaseFragmentActivity>(activity);
		}

		@Override
		public void handleMessage(Message mes) {
			BaseFragmentActivity baseFragmentActivity = softReference.get();

			switch (mes.what) {
				case 0:
//					if (!BaseApplication.getInstance().getIBLE().isEnable()){
//
//						break;
//					}
//					BaseApplication.getInstance().getIBLE().connect(m_nowMac, baseFragmentActivity);
					break;
				case 1:

					break;
				case 2:
					break;
				case 3:
					break;
				case 9:
					break;
				case 0x99://搜索超时
//					BaseApplication.getInstance().getIBLE().connect(m_nowMac, baseFragmentActivity);
					break;
				default:
					break;
			}
//			return false;
		}
	}
}
